#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :  tkdemo_Sign.py
@Desc    :  使用tkinter实现登录注册demo
@Author  :  Kearney
@Contact :  191615342@qq.com
@Version :  0.0.0
@License :  GPL-3.0
@Time    :  2020/10/20 11:25:23
'''
from tkinter import *
import configparser

file = 'config.ini'

#  登录
def btn_click_sign():
    userName = name.get()
    password = pwd.get()
    if len(userName)==0 or len(password)==0:
        print("请正确填写，不可为空")
        return
    cfg = configparser.ConfigParser()
    cfg.read(file, encoding='utf-8')
    if not cfg.has_section("User"): 
        print("缺乏配置文件，请联系作者")
    else:
        if cfg.has_option('User', userName):
            teppwd = cfg.get('User', userName)
            if teppwd == password:
                print("登录成功")
            else:
                print("登录失败，密码错误")
        else:
            print("登录失败，用户名不存在")

#  注册
def btn_click_log():
    userName = name.get()
    password = pwd.get()
    if len(userName)==0 or len(password)==0:
        print("请正确填写，不可为空")
        return
    cfg = configparser.ConfigParser()
    cfg.read(file, encoding='utf-8')
    if not cfg.has_section("User"):  
        cfg.add_section('User')
    if cfg.has_option('User', userName):
        print("注册失败，用户名已存在")
    else:
        cfg.set('User', userName, password)
        with open(file, 'w', encoding='utf-8') as configfile:
            cfg.write(configfile)
        print("注册成功")


root = Tk()
root.title('Sign_demo')
root.geometry('500x200')

pname = PanedWindow(root)
# pname.pack(fill = BOTH, expand = 1)
pname.pack(fill=BOTH)
lab_name = Label(pname, text='Name', font=('Arial', 14), width=15, height=2)
tv_name = StringVar()
name = Entry(pname, show=None, font=('Arial', 14), textvar = tv_name)
tv_name.set('Put your name here')


pname.add(lab_name)
pname.add(name)

ppwd = PanedWindow(root)
ppwd.pack(fill=BOTH)
lab_pwd = Label(ppwd, text='Password', font=('Arial', 14), width=15, height=2)
pwd = Entry(ppwd, show='*', font=('Arial', 14))
ppwd.add(lab_pwd)
ppwd.add(pwd)

pbtn = PanedWindow(root)
pbtn.pack(fill=BOTH, expand =1)
log = Button(pbtn,
             text='Sign up',
             font=('Arial', 14),
             bg='grey',
             width = 20,
             command=btn_click_log)
sign = Button(pbtn,
              text='Sign in',
              font=('Arial', 14),
              bg='lightblue',
              command=btn_click_sign)
pbtn.add(log)
pbtn.add(sign)

root.mainloop()